package chloroform.json.comp

import scaladuct.PropertyValue
import chloroform.json.{JsonBuilderFactory, CookiePropertyProcessingContext, GenericFactoryForElement}
import argo.jdom.JsonRootNode
import util.GenericAnalysisUtils

/**
 * User: zslajchrt
 * Date: 30.5.11
 * Time: 21:59
 */

object References {
  def getConstructors =
    List(new ReferencePropertyInfoConstructor)
}

class PropertyRefInfo extends JsonComposite {
  val refType = string()
}

class ReferencePropertyInfoHolder extends JsonComposite {
  val RefInfo = struct(new PropertyRefInfo())
}

class ReferencePropertyInfoConstructor
  extends GenericFactoryForElement[PropertyValue[_], CookiePropertyProcessingContext, JsonBuilderFactory[JsonRootNode]] {

  def create(property: PropertyValue[_], ctx: CookiePropertyProcessingContext) = {
    if (property.isReference) {
      val refInfoHolder = new ReferencePropertyInfoHolder()
      refInfoHolder.RefInfo().refType <= GenericAnalysisUtils.toString(property.refTypeInfo.getType)
      Some(refInfoHolder)
    } else {
      None
    }
  }

}
